package com.assignwork.service.impl;

import com.assignwork.dto.DailyPlanDTO;
import com.assignwork.dto.LineAuditDTO;
import com.assignwork.entity.DailyPlan;
import com.assignwork.mapper.DailyPlanMapper;
import com.assignwork.service.IDailyPlanService;
import com.assignwork.vo.AssemblyLineVO;
import com.assignwork.vo.DailyPlanMobileVO;
import com.assignwork.vo.DailyPlanVO;
import com.assignwork.vo.StartedCabinVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 日计划实现类
 */
@Service
public class DailyPlanServiceImpl extends BaseServiceImpl<DailyPlanMapper, DailyPlan> implements IDailyPlanService {

    @Override
    public List<DailyPlanVO> listDailyPlan(BladeUser bladeUser, Query query, DailyPlanDTO search) {
        List<OrderItem> order = Condition.getOrder(query);
        order.add(OrderItem.asc("ship_berth_key"));
        List<DailyPlanVO> dailyPlanVOS = this.baseMapper.listDailyPlan(search, order);
        for (DailyPlanVO vo : dailyPlanVOS) {
            vo.setPackageBulkName(DictBizCache.getValue(DailyPlanDTO.PACKAGE_BULK, vo.getPackageBulkKey()));
            vo.setImpExpName(DictBizCache.getValue(DailyPlanDTO.I_E_TYPE, vo.getImpExp()));
            vo.setWorkTypeName(DictBizCache.getValue(DailyPlanDTO.LOAD_TYPE, vo.getWorkTypeKey()));
            vo.setShipBerthName(DictBizCache.getValue(DailyPlanDTO.BERTH, vo.getShipBerthKey()));
        }
        return dailyPlanVOS;
    }

    @Override
    public List<DailyPlanVO> listDailyPlan4sd(BladeUser bladeUser, DailyPlanDTO dto) {
        List<DailyPlanVO> dailyPlanVOS = this.baseMapper.listDailyPlan4sd(dto);
        for (DailyPlanVO vo : dailyPlanVOS) {
            vo.setPackageBulkName(DictBizCache.getValue(DailyPlanDTO.PACKAGE_BULK, vo.getPackageBulkKey()));
            vo.setImpExpName(DictBizCache.getValue(DailyPlanDTO.I_E_TYPE, vo.getImpExp()));
            vo.setWorkTypeName(DictBizCache.getValue(DailyPlanDTO.LOAD_TYPE, vo.getWorkTypeKey()));
            vo.setShipBerthName(DictBizCache.getValue(DailyPlanDTO.BERTH, vo.getShipBerthKey()));

            for (AssemblyLineVO assemblyLineVO : vo.getAssemblyLine()) {
                assemblyLineVO.setShiftKeyName(DictBizCache.getValue("time_space", assemblyLineVO.getShiftKey()));
                assemblyLineVO.setLoadTypeName(DictBizCache.getValue("load_type", assemblyLineVO.getLoadType()));
                assemblyLineVO.setPackageBulkName(DictBizCache.getValue("package_bulk", assemblyLineVO.getPackageBulkKey()));

                for (StartedCabinVO startedCabinVO : assemblyLineVO.getAllStartedCabin()) {
                    String hatch = startedCabinVO.getHatch();
                    String hatchName = "";
                    if (!hatch.equals("ALL")) {
                        hatchName = DictBizCache.getValue("hold_position", hatch);
                    }
                    startedCabinVO.setCabinName(DictBizCache.getValue("hold", startedCabinVO.getCabin()));
                    startedCabinVO.setHatchName(hatchName);
                    startedCabinVO.setCabinAndHatchName(startedCabinVO.getCabinName() + hatchName);
                }
            }
        }
        return dailyPlanVOS;
    }

    @Override
    public List<DailyPlanMobileVO> listDailyPlan4Mobile(IPage page, LineAuditDTO dto) {
        List<DailyPlanMobileVO> ls = this.baseMapper.listDailyPlan4Mobile(page, dto);

        for (DailyPlanMobileVO vo : ls) {
            vo.setPackageBulkName(DictBizCache.getValue(DailyPlanDTO.PACKAGE_BULK, vo.getPackageBulkKey()));
            vo.setImpExpName(DictBizCache.getValue(DailyPlanDTO.I_E_TYPE, vo.getImpExp()));
            vo.setWorkTypeName(DictBizCache.getValue(DailyPlanDTO.LOAD_TYPE, vo.getWorkTypeKey()));
            vo.setShipBerthName(DictBizCache.getValue(DailyPlanDTO.BERTH, vo.getShipBerthKey()));
            String[] cs = Func.toStrArray(vo.getLineCabins());
            ArrayList<String> cns = new ArrayList<>();
            for (String c : cs) {
                cns.add(DictBizCache.getValue(DailyPlanDTO.HOLD, c));
            }
            vo.setLineCabinNames(Func.join(cns, ","));
        }

        return ls;
    }

}